#coding:utf-8

import base
from base import CUSTOM
from base import PASSED, FAILED, WARN

#103d 配置无线下载
class Buss103d(base.Buss):
    name = u"请求无线下载"
    busstype = [0x10, 0x3d]
    
    dx = {u'非压缩模式下载': 0x00,
          u'压缩模式下载'  : 0x01,
        }
        
    dk = {u'不立即下载' :0x00,
         u'立即下载' :0x01,
        }
        
    der = {u'不擦除参数区': 0x00,
          u'擦除参数区': 0x01,
          }
        
    dtype = {u'无下载' :0x00,
            u'主程序' : 0x01,
            u'Boot程序' :0x02,
            u'调度屏程序' : 0x03,
            }
            
    dmode = {u'TCP' : 0x00,
            u'UDP' : 0x01,
            }
          
    def init(self):  
        self.compressed = 0x01
        self.termed = 0x00
        self.erased = 0x00
        self.program = 0x01
        self.mode = 0x01
        self.apn = [0x20] * 20
        self.ip = [0x20] * 20
        self.port = [0x20] * 5
        self.vLength = [0x20] * 20
        self.newVersion = [0x20] * 30
        self.time = []
        
    def separate(self, data):
        property = base.pickUp(data.get(1))
        self.compressed = property.get(1, 1)
        self.termed = property.get(2, 2)
        self.erased = property.get(3, 3)
        self.program = property.get(4, 7)
        self.mode = data.get(1)
        self.apn = data.get(20)
        self.ip = data.get(20)
        self.port = data.get(5)
        self.vLength = 0x00
        self.newVersion = data.get(30)
        self.time = data.get(2)
        
    def assemble(self, data):
        property = base.pickUp(0)
        property.set(self.compressed, 1, 1)
        property.set(self.termed, 2, 2)
        property.set(self.erased, 3, 3)
        property.set(self.program, 4, 7)
        args = (property.out(), self.mode, self.apn, self.ip, 
                self.port, self.vLength, self.newVersion)
        data.set(*args)
        if self.termed == 0x0:
            data.set(self.time)
        
    def getDownloadMode(self):
        return base.getKey(self.dx, self.compressed)
        
    def setDownloadMode(self, value):
        self.compressed = self.dx[value]
    
    def getDownloadIm(self):
        return base.getKey(self.dk, self.termed)
    
    def setDownloadIm(self, value):
        self.termed = self.dk[value]
        
    def getDownloadErase(self):
        return base.getKey(self.der, self.erased)
            
    def setDownloadErase(self, value):
        self.erased = self.der[value]
        
    def getDownloadProgram(self):
        return base.getKey(self.dtype, self.program)
            
    def setDownloadProgram(self, value):
        self.program = self.dtype[value]
        
    def getMode(self):
        return base.getKey(self.dmode, self.mode)
            
    def setMode(self, value):
        self.mode = self.dmode[value]
        
    def getAPN(self):
        return base.data2unicode(self.apn)
    
    def setAPN(self, value):
        x = base.unicode2data(value)
        self.apn = x + [0x20] * (20 - len(x))

    def getIP(self):
        return base.data2unicode(self.ip)
    
    def setIP(self, value):
        x = base.unicode2data(value)
        self.ip = x + [0x20] * (20 - len(x))
        
    def getPort(self):
        return base.data2unicode(self.port)
    
    def setPort(self, value):
        x = base.unicode2data(value)
        self.port = x + [0x20] * (5 - len(x))
        
    def getNewVerSion(self):
        return base.data2unicode(self.newVersion)
        
    def setNewVerSion(self, value):
        x = base.unicode2data(value)
        self.newVersion = x + [0x20] * (30 - len(x))
        
    def getTime(self):
        return "%d:%d"%(self.time[0], self.time[1])
        
    def setTime(self, value):
        hour = value[:2]
        min = value[-2:]
        self.time = [int(hour), int(min)]
        
    def getList(self):
        return [
                [u'是否压缩下载', self.dx.keys(), self.getDownloadMode, self.setDownloadMode],
                [u'是否立即下载', self.dk.keys(), self.getDownloadIm, self.setDownloadIm],
                [u'是否擦除参数', self.der.keys(), self.getDownloadErase, self.setDownloadErase],
                [u'程序类别', self.dtype.keys(), self.getDownloadProgram, self.setDownloadProgram],
                [u'模式', self.dmode.keys(), self.getMode, self.setMode],
                [u'APN', '', self.getAPN, self.setAPN],
                [u'IP', '', self.getIP, self.setIP],
                [u'PORT', '', self.getPort, self.setPort],
                [u'新版本号', '', self.getNewVerSion, self.setNewVerSion],
                ]
    
    def play(self, hunter):
        hunter.sendData(self)
        hunter.setDownload()

#107d 无线下载配置应答
class Buss107d(base.Buss):
    busstype = [0x10, 0x7d]
    
    dr = {u'成功' : 0x01,
          u'失败' : 0x02,
        }
    def init(self):
        self.response = 0x01
    def separate(self, data):
        self.response = data.get(1)
        
    def assemble(self, data):
        data.set(self.response)
        
    def getResponse(self):
        return base.getKey(self.dr, self.response)
    
    def setResponse(self, value):
        self.program = self.dr[value]
        
    def getList(self):
        return [
                [u'应答类型', self.dr.keys(), self.getResponse, self.setResponse],
                ]
    
    def play(self, hunter):
        hunter.startDownload()


#1089 车台更新程序应答
class Buss1089(base.Buss):
    busstype = [0x10, 0x89]
    
    dr = {u'成功' : 0x01,
          u'失败' : 0x02,
        }
    def init(self):
        self.response = 0x01
    def separate(self, data):
        self.response = data.get(1)
        
    def assemble(self, data):
        data.set(self.response)
        
    def getResponse(self):
        return base.getKey(self.dr, self.response)
    
    def setResponse(self, value):
        self.program = self.dr[value]
        
    def getList(self):
        return [
                [u'应答类型', self.dr.keys(), self.getResponse, self.setResponse],
                ]
    
    def play(self, hunter):
        if self.getValue(0) == 0x01:
            hunter.finishDownload(u'下载完成')
        else:
            hunter.finishDownload(u'下载失败')


DOWNLOAD_SET = 'setDownload'
DOWNLOAD_RESPONSE = 'response'
DOWNLOAD_FINISH = 'finish'
DOWNLOAD_RESULT = 'downResult'
DOWNLOAD_USETIME = 'useTime'

class Hunter(base.HunterMaster):
    """
    columns : request, start, finish, downloadResult
    
    """
    name = u'千里眼无线下载功能'
    operation = (Buss103d,)
    wanted = (Buss103d, Buss107d, Buss1089)
    def init(self):

        #self.createTable(DOWNLOAD_SET, DOWNLOAD_RESPONSE, DOWNLOAD_FINISH, 
        #               DOWNLOAD_RESULT)
        self.data = {}
    
    def setDownload(self):

        #self.data[DOWNLOAD_SET] = self.now()
        pass

        
    def startDownload(self):

        if self.data.get(DOWNLOAD_RESPONSE, None):
            # 数据多余
            return
        if self.data.get(DOWNLOAD_SET, None):
            self.data[DOWNLOAD_RESPONSE] = self.now()
        else:
            # 数据多余
            return

        
    def finishDownload(self, result):

        if self.data.get(DOWNLOAD_FINISH, None):
            # 数据多余
            return
        #self.data[DOWNLOAD_FINISH] = self.now()
        self.data[DOWNLOAD_RESULT] = result
        self.nextTest()

